package org.symkom.card;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.symkom.calculation.Function;
import org.symkom.graph.Graph;
import org.symkom.main.MainWindow;
import org.symkom.states.States;

public class GraphPane extends Card{

	private Function f;
	private Graph graph;
	
	private JPanel titlePane;
	private JPanel middlePane;
	private JPanel navigationPane;
	
	private JPanel midPane;
	
	private JTextField tfX0;
	private JTextField tfT0;
	private JTextField tfStep;
	private JTextField tfNumber;
	
	public GraphPane(MainWindow mainWindow, Card prevCard) {
		super(mainWindow, prevCard);
		
		
		
	}
	
	public void init() {
		super.init();
		setLayout(new BorderLayout());
		
		titlePane = new JPanel(new FlowLayout());
		middlePane = new JPanel(new FlowLayout());
		navigationPane = new JPanel(new FlowLayout());
		
		titlePane.add(new JLabel("Wykres funkcji: "));
		
		midPane = new JPanel(new BorderLayout());
		
		JPanel fieldsPane = new JPanel(new GridLayout(1, 9, 10, 10));
		JLabel lX0 = new JLabel("x0: ");
		JLabel lT0 = new JLabel("t0: ");
		JLabel lStep = new JLabel("dt: ");
		JLabel lNumber = new JLabel("n: ");
		
		tfX0 = new JTextField("0.0");
		tfT0 = new JTextField("0.0");
		tfStep = new JTextField("0.5");
		tfNumber = new JTextField("500");
		
		JButton button = new JButton("Rysuj");
		button.addActionListener(new DrawGraph());
		
		fieldsPane.add(lX0);
		fieldsPane.add(tfX0);
		fieldsPane.add(lT0);
		fieldsPane.add(tfT0);
		fieldsPane.add(lStep);
		fieldsPane.add(tfStep);
		fieldsPane.add(lNumber);
		fieldsPane.add(tfNumber);
		fieldsPane.add(button);
		midPane.add(fieldsPane, BorderLayout.NORTH);
		
		JPanel bottomPane = new JPanel(new FlowLayout());
		JButton xPlus = new JButton("+");
		JButton xNormal = new JButton("100%");
		JButton xMinus = new JButton("-");
		JButton yPlus = new JButton("+");
		JButton yNormal = new JButton("100%");
		JButton yMinus = new JButton("-");
		
		bottomPane.add(xPlus);
		bottomPane.add(xNormal);
		bottomPane.add(xMinus);
		bottomPane.add(yPlus);
		bottomPane.add(yNormal);
		bottomPane.add(yMinus);
		
		xPlus.addActionListener(new XPlusListener());
		xNormal.addActionListener(new XNormalListener());
		xMinus.addActionListener(new XMinusListener());
		yPlus.addActionListener(new YPlusListener());
		yNormal.addActionListener(new YNormalListener());
		yMinus.addActionListener(new YMinusListener());
		
		midPane.add(bottomPane, BorderLayout.SOUTH);
		
		graph = new Graph(800,600);
		f = new Function(function);
		graph.setPoints(f.metodaAdamsBashford(0., 0.));
		midPane.add(graph, BorderLayout.CENTER);
		
		middlePane.add(midPane);
		
		navigationPane.add(nextButton);
		
		add(titlePane, BorderLayout.NORTH);
		add(middlePane, BorderLayout.CENTER);
		add(navigationPane, BorderLayout.SOUTH);
	}
	
	@Override
	protected void nextStage() {
		mainWindow.setState(States.FUNCTION, this);
		
	}

	class DrawGraph implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			double x0 = 0.;
			double t0 = 0.;
			
			try {
				double step = Double.parseDouble(tfStep.getText());
				f.setDt(step);
				int number = Integer.parseInt(tfNumber.getText());
				f.setLimit(number);
				
				x0 = Double.parseDouble(tfX0.getText());
				t0 = Double.parseDouble(tfT0.getText());
			} catch (NumberFormatException ex) {
				JOptionPane.showMessageDialog(null, "Nieprawidlowy format liczb!",
						"Blad", JOptionPane.ERROR_MESSAGE); 
				return;
			}
			
			List<Point2D> list = f.metodaAdamsBashford(x0, t0);
			graph.setPoints(list);
			if(list.isEmpty()) JOptionPane.showMessageDialog(null, 
					"Nie mozna znalezc rozwiazania dla podanego warunku poczatkowego.",
					"Informacja",
					JOptionPane.INFORMATION_MESSAGE); 
			revalidate();
			repaint();
		}
		
	}
	
	class ScaleListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			revalidate();
			repaint();
		}
		
	}
	
	class XPlusListener extends ScaleListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Graph.scaleX *= Graph.SCALE_STEP;
			super.actionPerformed(arg0);
		}
		
	}
	
	class XNormalListener extends ScaleListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Graph.scaleX = Graph.SCALE_X;
			super.actionPerformed(arg0);
		}
		
	}
	
	class XMinusListener extends ScaleListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Graph.scaleX /= Graph.SCALE_STEP;
			super.actionPerformed(arg0);
		}
		
	}
	
	class YPlusListener extends ScaleListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Graph.scaleY *= Graph.SCALE_STEP;
			super.actionPerformed(arg0);
		}
		
	}
	
	class YNormalListener extends ScaleListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Graph.scaleY = Graph.SCALE_Y;
			super.actionPerformed(arg0);
		}
		
	}
	
	class YMinusListener extends ScaleListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Graph.scaleY /= Graph.SCALE_STEP;
			super.actionPerformed(arg0);
		}
		
	}
}
